package com.hoshiicloud.goods.feign;

import com.hoshiicloud.common.query.UploadFileResponse;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.goods.feign.hystrix.UploadFileFeignFallback;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 *  文件上传
 * @author linjx on 2019/8/2
 */

@FeignClient(value = "hoshiicloud-fileserver", configuration = UpLoadFileService.MultipartSupportConfig.class, fallback = UploadFileFeignFallback.class)
public interface UpLoadFileService {

    @RequestMapping(method = RequestMethod.POST, value = "/file/uploadOneFile",
            produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
     Response uploadFile(@RequestPart(value = "file") MultipartFile file, @RequestParam(value = "bucketName") String bucketName,@RequestParam("objectKey") String objectKey,@RequestParam("fileName") String fileName,@RequestParam("folder") String folder,@RequestParam("privateOwned") Boolean privateOwned);


    @PostMapping(value = "/file/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Response<UploadFileResponse> fileUpload(@RequestParam("bucketName") String bucketName,
                                            @RequestParam("objectKey") String objectKey, @RequestParam("fileName") String fileName,
                                            @RequestParam("folder") String folder,
                                            @RequestParam("privateOwned") Boolean privateOwned, @RequestPart("file") MultipartFile file);

    class MultipartSupportConfig {

        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}
